<?php
	/**
	* Shopping Cart View
	* 
	* @author trungdt
	* @since Oct 19, 2011
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');
	
	exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
	
	$template=new template("productsmodule","_cart_view");
	if (!defined("ABProductsModule_Carts"))
		define("ABProductsModule_Carts","ABShopCart_SessionKey");

	$carts=exponent_sessions_get(ABProductsModule_Carts);
	if ($carts == null)
		$carts = array();
	
	$currency=null; // hhee, trick to save last config, for currency format to cart total
	$cart_count=0;
	$cart_total=0;
	$cart_total_saving=0;
	foreach ($carts as $v)
	{
		$cart_count+=$v->quantity;
		// get config
		$config=$db->selectObject("productsmodule_config", "location_data = '{$v->location_data}'");
		$currency=productsmodule::_GetCurrencyDefault($config->currency_id);
		// calculate total of this cart
		$temp=$v->quantity;
		if ($v->on_sale)
			$temp*=$v->price_sale_bk;
		else
			$temp*=$v->price_bk;
		$cart_total_saving+=$v->price_save_bk * $v->quantity;
		$cart_total+=$temp;
		$v->price_total_value=$temp;
		$temp=productsmodule::_GetCurrencyFormat($currency, $temp);
		$v->price_total=$temp;
	}
	$cart_tax=0.1*$cart_total;
	$cart_total2 = $cart_tax + $cart_total;
	$cart_tax=productsmodule::_GetCurrencyFormat($currency, $cart_tax);
	$cart_total = productsmodule::_GetCurrencyFormat($currency, $cart_total);
	// total included tax
	$cart_total2 = productsmodule::_GetCurrencyFormat($currency, $cart_total2);
	$cart_total_saving_bk=$cart_total_saving;
	$cart_total_saving = productsmodule::_GetCurrencyFormat($currency, $cart_total_saving);
	$cart_shipping = productsmodule::_GetCurrencyFormat($currency, 0);
	
	// for shipping 
	// update the shipping info
	// we should determine what method will we use
	$shipping_registered=null;
	$shipping_manual=null;
	productsmodule::__Get_User_Shipping($shipping_registered, $shipping_manual);
	
	$template=new template("productsmodule","_cart_view");
	$template->assign("carts", $carts); 
	$template->assign("cart_total", $cart_total); 
	$template->assign("cart_total2", $cart_total2); 
	$template->assign("cart_total_saving_bk", $cart_total_saving_bk); 
	$template->assign("cart_total_saving", $cart_total_saving); 
	$template->assign("cart_tax", $cart_tax); 
	$template->assign("cart_shipping", $cart_shipping); 
	// shipping
	$template->assign("info1", $shipping_registered); 
	$template->assign("info2", $shipping_manual); 
	$template->assign("is_logged", exponent_users_isLoggedIn()?1:0); 
	$template->assign("not_use_info1", ($shipping_registered->info1_name=="" || $shipping_registered->info1_address == "" || $shipping_registered->info1_phone == "")?1:0); 
	
	_ab_theme_action_register_title(exponent_lang_getText("cart_view"));
	
	$template->output();
?>
